#include "utexteditwidget.h"
#include <QTextEdit>
#include <QPushButton>
#include "uclientserver.h"
#include <QJsonObject> // { }
#include <QJsonArray> // [ ]
#include <QJsonDocument> // 解析Json
#include <QJsonValue> // int float double bool null { } [ ]
#include <QJsonParseError>// jsonError.error != QJsonParseError::NoError

#include "uaddbookwidget.h"
#include "uquerybookwidget.h"
#include "udeletebookwidget.h"
#include "urevisebookwidget.h"

UTextEditWidget::UTextEditWidget(QWidget *parent)
	: QWidget(parent)
	, m_pTextEdit(new QTextEdit(this))
	, m_pSendButton(new QPushButton(this))
	, m_pGetJsonButton(new  QPushButton(this))
	, m_pUpdateJsonButton(new QPushButton(this))
    , m_pManageVersionButton(new QPushButton(this))
    , m_pAddBookWidget(new UAddBookWidget)
    , m_pQueryBookWidget(new UQueryBookWidget)
    , m_pDeleteBookWidget(new UDeleteBookWidget)
    , m_pReviseBookWidget(new UReviseBookWidget)
    , m_pAddBooksButton(new QPushButton(this))
    , m_pQueryBooksButton(new QPushButton(this))
    , m_pDeleteBooksButton(new QPushButton(this))
    , m_pReviseBooksButton(new QPushButton(this))

{
	setAttribute(Qt::WA_StyledBackground, true);//默认Qwidget类 无法设置颜色
	setFixedSize(850, 240);
	setStyleSheet(R"(UTextEditWidget{border:none;})");

	initButton();
	initConnect();
   /* m_pAddBookWidget->hide();
    m_pQueryBookWidget->hide();
    m_pDeleteBookWidget->hide();*/
}

UTextEditWidget::~UTextEditWidget()
{
    delete m_pAddBookWidget; //m_pAddBookWidget没有指定父对象，需要手动释放和置空。或者定义为QPointer
    m_pAddBookWidget = nullptr;

    delete m_pQueryBookWidget; //m_pQueryBookWidget没有指定父对象，需要手动释放和置空。或者定义为QPointer
    m_pQueryBookWidget = nullptr;

    delete m_pDeleteBookWidget; //m_pDeleteBookWidget没有指定父对象，需要手动释放和置空。或者定义为QPointer
    m_pDeleteBookWidget = nullptr;

    delete m_pReviseBookWidget; //m_pDeleteBookWidget没有指定父对象，需要手动释放和置空。或者定义为QPointer
    m_pReviseBookWidget = nullptr;
}

void UTextEditWidget::initButton()
{
	m_pTextEdit->resize(850, 150);
	m_pTextEdit->move(0, 30);
	m_pTextEdit->setStyleSheet(R"(QTextEdit{background-color:#F5F5F5;border:none;font-family:微软雅黑;font-size: 12px;})");

    QString ButtonStyleSheet = R"(QPushButton{background-color:#808080;border:none;font:#000000;font-family:微软雅黑;font-size: 12px;}
									QPushButton:hover{background-color:#E9E9E9;})";

	m_pSendButton->resize(80, 35);
	m_pSendButton->move(700, 180);
	m_pSendButton->setText("Send");
	m_pSendButton->setStyleSheet(ButtonStyleSheet);

	m_pGetJsonButton->resize(50, 30);
	m_pGetJsonButton->move(5, 0);
	m_pGetJsonButton->setText("GetJson");
	m_pGetJsonButton->setStyleSheet(ButtonStyleSheet);

	m_pUpdateJsonButton->resize(50, 30);
	m_pUpdateJsonButton->move(60, 0);
	m_pUpdateJsonButton->setText("UpdJson");
	m_pUpdateJsonButton->setStyleSheet(ButtonStyleSheet);

    m_pManageVersionButton->resize(50, 30);
    m_pManageVersionButton->move(115, 0);
    m_pManageVersionButton->setText("MagVers");
    m_pManageVersionButton->setStyleSheet(ButtonStyleSheet);

    m_pAddBooksButton->resize(60, 30);
    m_pAddBooksButton->move(250, 0);
    m_pAddBooksButton->setText("AddBook");
    m_pAddBooksButton->setStyleSheet(ButtonStyleSheet);

    m_pQueryBooksButton->resize(60, 30);
    m_pQueryBooksButton->move(315, 0);
    m_pQueryBooksButton->setText("QueryBook");
    m_pQueryBooksButton->setStyleSheet(ButtonStyleSheet);

    m_pDeleteBooksButton->resize(60, 30);
    m_pDeleteBooksButton->move(380, 0);
    m_pDeleteBooksButton->setText("DelBook");
    m_pDeleteBooksButton->setStyleSheet(ButtonStyleSheet);

    m_pReviseBooksButton->resize(60, 30);
    m_pReviseBooksButton->move(445, 0);
    m_pReviseBooksButton->setText("RevBook");
    m_pReviseBooksButton->setStyleSheet(ButtonStyleSheet);
}

void UTextEditWidget::initConnect()
{
	connect(m_pSendButton, &QPushButton::released, this, &UTextEditWidget::onSendButtonReleased);
	connect(m_pGetJsonButton, &QPushButton::released, this, &UTextEditWidget::onGetJsonButtonReleased);
	connect(m_pUpdateJsonButton, &QPushButton::released, this, &UTextEditWidget::onUpdataJsonButtonReleased);
    connect(m_pManageVersionButton, &QPushButton::released, this, &UTextEditWidget::onManageVersionButtonReleased);
    connect(m_pAddBooksButton, &QPushButton::released, this, &UTextEditWidget::onAddBooksButtonReleased);
    connect(m_pQueryBooksButton, &QPushButton::released, this, &UTextEditWidget::onQueryBooksButtonReleased);
    connect(m_pDeleteBooksButton, &QPushButton::released, this, &UTextEditWidget::onDeleteBooksButtonReleased);
    connect(m_pReviseBooksButton, &QPushButton::released, this, &UTextEditWidget::onReviseBooksButtonReleased);
}

QByteArray UTextEditWidget::getObjectInfo()
{
    // 创建一个根对象  
    QJsonObject root;

    // 创建number1对象  
    QJsonObject number1;
    number1["name"] = "categoriesname";

    // 创建一个categories数组  
    QJsonArray categories;

    // 创建第一个类别对象（Fiction）  
    QJsonObject fictionCategory;
    fictionCategory["categoryName"] = "Fiction";

    // 创建Fiction类别的books数组  
    QJsonArray fictionBooks;
    QJsonObject fictionBook1;
    fictionBook1["title"] = "1984";
    fictionBook1["author"] = "George Orwell";
    fictionBook1["isbn"] = "9780451524935";
    fictionBooks.append(fictionBook1);

    QJsonObject fictionBook2;
    fictionBook2["title"] = "To Kill a Mockingbird";
    fictionBook2["author"] = "Harper Lee";
    fictionBook2["isbn"] = "9780060935467";
    fictionBooks.append(fictionBook2);

    fictionCategory["books"] = fictionBooks;
    categories.append(fictionCategory);

    // 创建第二个类别对象（Non-Fiction）  
    QJsonObject nonFictionCategory;
    nonFictionCategory["categoryName"] = "Non-Fiction";

    // 创建Non-Fiction类别的books数组  
    QJsonArray nonFictionBooks;
    QJsonObject nonFictionBook1;
    nonFictionBook1["title"] = "Sapiens: A Brief History of Humankind";
    nonFictionBook1["author"] = "Yuval Noah Harari";
    nonFictionBook1["isbn"] = "9780062316097";
    nonFictionBooks.append(nonFictionBook1);

    QJsonObject nonFictionBook2;
    nonFictionBook2["title"] = "A Brief History of Time";
    nonFictionBook2["author"] = "Stephen Hawking";
    nonFictionBook2["isbn"] = "9780553109535";
    nonFictionBooks.append(nonFictionBook2);

    nonFictionCategory["books"] = nonFictionBooks;
    categories.append(nonFictionCategory);

    // 创建第三个类别对象（Science Fiction）  
    QJsonObject scienceFictionCategory;
    scienceFictionCategory["categoryName"] = "Science Fiction";

    // 创建Science Fiction类别的books数组  
    QJsonArray scienceFictionBooks;
    QJsonObject scienceFictionBook1;
    scienceFictionBook1["title"] = "Dune";
    scienceFictionBook1["author"] = "Frank Herbert";
    scienceFictionBook1["isbn"] = "9780441013593";
    scienceFictionBooks.append(scienceFictionBook1);

    QJsonObject scienceFictionBook2;
    scienceFictionBook2["title"] = "Neuromancer";
    scienceFictionBook2["author"] = "William Gibson";
    scienceFictionBook2["isbn"] = "9780451457992";
    scienceFictionBooks.append(scienceFictionBook2);

    scienceFictionCategory["books"] = scienceFictionBooks;
    categories.append(scienceFictionCategory);

    // 创建第四个类别对象（Children's Books）  
    QJsonObject childrensBooksCategory;
    childrensBooksCategory["categoryName"] = "Children's Books";

    // 创建Children's Books类别的books数组  
    QJsonArray childrensBooks;
    QJsonObject childrensBook1;
    childrensBook1["title"] = "Harry Potter and the Sorcerer's Stone";
    childrensBook1["author"] = "J.K. Rowling";
    childrensBook1["isbn"] = "9780747532699";
    childrensBooks.append(childrensBook1);

    QJsonObject childrensBook2;
    childrensBook2["title"] = "Charlotte's Web";
    childrensBook2["author"] = "E.B. White";
    childrensBook2["isbn"] = "9780060973106";
    childrensBooks.append(childrensBook2);

    childrensBooksCategory["books"] = childrensBooks;
    categories.append(childrensBooksCategory);

    // 将categories数组添加到number1对象中  
    number1["categories"] = categories;

    // 将number1对象添加到根对象中  
    root["number1"] = number1;

    // 将根对象转换为JSON文档
    QJsonDocument jsonDoc(root);

    // 返回
    return jsonDoc.toJson(QJsonDocument::Indented);
}

void UTextEditWidget::onUpdataJsonButtonReleased()
{
    UClientServer::Instance()->updateJson(getObjectInfo());
    emit sendText(QString("请求更新Json"));
}

void UTextEditWidget::onSendButtonReleased()
{
	auto text = m_pTextEdit->toPlainText();
	if (text.isEmpty())return;
	m_pTextEdit->clear();
	m_pTextEdit->setFocus();

	UClientServer::Instance()->sendMessage(text);
	emit sendText(text);
}

void UTextEditWidget::onGetJsonButtonReleased()
{
	UClientServer::Instance()->getDefaultJson();
}

void UTextEditWidget::onManageVersionButtonReleased()
{
    //出现界面，获取以往所有版本，可以解压，保存到客户端。
    qDebug() << __func__;
}

void UTextEditWidget::onAddBooksButtonReleased()
{
    m_pAddBookWidget->show();
}

void UTextEditWidget::onQueryBooksButtonReleased()
{
    m_pQueryBookWidget->show();
}

void UTextEditWidget::onDeleteBooksButtonReleased()
{
    m_pDeleteBookWidget->show();
}

void UTextEditWidget::onReviseBooksButtonReleased()
{
    m_pReviseBookWidget->show();
}